// 全局监听配置类
package com.mdp.workflow.cfg;

import com.google.common.collect.Maps;
import com.mdp.workflow.bus.listener.global.*;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.api.delegate.event.FlowableEventType;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * flowable 全局监听器配置类
 * @author: Lu Yang
 *@author 唛盟开源 9哥
 * @see FlowableEventType
 */
public class FlowableListenerConfig {

    // flowable监听级别参照 {@link FlowableEngineEventType} org.flowable.common.engine.api.delegate.event
    /**
     * 任务节点前置监听
     */
    public static final String TASK_COMPLETED = "TASK_COMPLETED";

    public static final String TASK_CREATED = "TASK_CREATED";
	
    public static final String PROCESS_COMPLETED = "PROCESS_COMPLETED";

    /**
     * A process instance has been created. All basic properties have been set, but variables not yet.
     * 变量还没初始化
     */
    public static final String PROCESS_CREATED = "PROCESS_CREATED";   

    /**
     *  变量已经初始化
     */
    public static final String  PROCESS_STARTED="PROCESS_STARTED";
    
    public static final String PROCESS_CANCELLED="PROCESS_CANCELLED";
    
    public static final String TASK_ASSIGNED="TASK_ASSIGNED";

    // 自己建立监听类实现FlowableEventListener接口
    /**
     * 任务节点前置监听
     */
	 @Autowired
    private TaskCreatedListener taskCreatedListener;
	 
    /**
     * 任务节点完成后监听
     */
	 @Autowired
    private TaskCompletedListener taskCompletedListener;
    /**
     * 任务节点完成后监听
     */
	 @Autowired
    private TaskAssignedListener taskAssignedListener;
    
    /**
     * 任务节点前置监听
     */
    @Autowired
    private ProcessCompletedListener processCompletedListener;
    
    /**
     * 任务节点前置监听
     */
    @Autowired
    private ProcessStartedListener processStartedListener;
    

    /**
     * 任务节点前置监听
     */
    @Autowired
    private  ProcessCancelledListener processCancelledListener;


   
    public FlowableListenerConfig() { 
    }

    /**
     * 将自定义监听器纳入flowable监听
     * @author: Lu Yang
     * @date: 2019/5/4 21:05
     * @param
     * @return org.flowable.spring.boot.EngineConfigurationConfigurer<org.flowable.spring.SpringProcessEngineConfiguration>
     */
    @Bean
    public EngineConfigurationConfigurer<SpringProcessEngineConfiguration> globalListenerConfigurer () {
        return engineConfiguration -> {
            engineConfiguration.setTypedEventListeners(this.customFlowableListeners());
        };
    }

    /**
     * 监听类配置 {@link FlowableEngineEventType} flowable监听器级别
     * @author: Lu Yang
     * @date: 2019/5/4 20:58
     * @param
     * @return java.util.Map<java.lang.String,java.util.List<org.flowable.common.engine.api.delegate.event.FlowableEventListener>>
     */
    private Map<String, List<FlowableEventListener>> customFlowableListeners () {
        Map<String, List<FlowableEventListener>> listenerMap = Maps.newHashMap();
        listenerMap.put(TASK_CREATED, new ArrayList<>(Collections.singletonList(taskCreatedListener)));
        listenerMap.put(TASK_COMPLETED, new ArrayList<>(Collections.singletonList(taskCompletedListener))); 
        listenerMap.put(TASK_ASSIGNED, new ArrayList<>(Collections.singletonList(taskAssignedListener)));
        listenerMap.put(PROCESS_COMPLETED, new ArrayList<>(Collections.singletonList(processCompletedListener)));
        listenerMap.put(PROCESS_STARTED, new ArrayList<>(Collections.singletonList(processStartedListener))); 
        listenerMap.put(PROCESS_CANCELLED, new ArrayList<>(Collections.singletonList(processCancelledListener))); 
        
		return listenerMap;
    }

    public TaskCreatedListener getTaskBeforeListener() {
        return taskCreatedListener;
    }
}